|
|
|
|
|
|
|
Figure 8.20.
Using the DISTINCT keyword to
remove duplicates from a result set. |
|
|
|
|
|
|
|
|
If you include more than one column in the SELECT list, all columns are used to evaluate the uniqueness of the row. Execute and compare the result sets of the following two SQL statements. Refer to Figure 8.21 as a guide. |
|
|
|
|
|
|
|
|
SELECT DISTINCT Title
FROM BookSales |
|
|
|
|
|
|
|
|
SELECT DISTINCT Title, Units
FROM BookSales |
|
|
|
|
|
|
|
|
Figure 8.21.
Using DISTINCT on multiple columns. |
|
|
|
|
|
|
|
|
Notice that the first SQL statement returns a single record for each Title in the data table. The second SQL statement returns more records for each Title because there are distinct Units values for each Title. |
|
|
|
|
|
|
|
|
There are also times when you want to collect data on all rows that are distinct in any of the fields. Instead of using the DISTINCT keyword and listing all the fields in the table, you can |
|
|
|
|
|